/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.dbm;

import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Value;

/**
 * This interface defines a Dbm implementation.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
public interface DbmImplementation {

    /**
     * Opens a database.
     * @param ctxt context
     * @param file database file
     * @param flags allowed database operations
     * @param mode file permissions for created file
     * @return database handle
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the file cannot be
     *                        opened/created
     */
    Value open(CodeRunner ctxt, Value file, Value flags, Value mode)
        throws Fail.Exception, FalseExit;

    /**
     * Closes a database.
     * @param ctxt context
     * @param db database handle
     * @return <i>unit</i>
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     */
    Value close(CodeRunner ctxt, Value db)
        throws Fail.Exception, FalseExit;

    /**
     * Gets a value using its key.
     * @param ctxt context
     * @param db database handle
     * @param key key
     * @return data associated with passed key
     * @throws Fail.Exception <i>Not_found</i> if no entry exists for the
     *                        passed key
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if an i/o error occurs
     */
    Value fetch(CodeRunner ctxt, Value db, Value key)
        throws Fail.Exception, FalseExit;

    /**
     * Inserts a (key, data) pair in a database.
     * @param ctxt context
     * @param db database handle
     * @param key key
     * @param content data associated with key
     * @return <i>unit</i>
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if an i/o error occurs
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if an entry with the same
     *                        key already exists
     */
    Value insert(CodeRunner ctxt, Value db, Value key, Value content)
        throws Fail.Exception, FalseExit;

    /**
     * Inserts a (key, data) pair in a database, replacing current value
     * if a pair with same key already exists.
     * @param ctxt context
     * @param db database handle
     * @param key key
     * @param content data associated with key
     * @return <i>unit</i>
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if an i/o error occurs
     */
    Value replace(CodeRunner ctxt, Value db, Value key, Value content)
        throws Fail.Exception, FalseExit;

    /**
     * Deletes a value using its key.
     * @param ctxt context
     * @param db database handle
     * @param key key
     * @return <i>unit</i>
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if an i/o error occurs
     */
    Value delete(CodeRunner ctxt, Value db, Value key)
        throws Fail.Exception, FalseExit;

    /**
     * Returns the first key of a database.
     * @param ctxt context
     * @param db database handle
     * @return the first key of the passed database
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if such a key does not
     *                        exist
     */
    Value firstkey(CodeRunner ctxt, Value db)
        throws Fail.Exception;

    /**
     * Returns the next key of a database.
     * @param ctxt context
     * @param db database handle
     * @return the next key of the passed database
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if the database is already
     *                        closed
     * @throws Fail.Exception <i>Dbm.Dbm_error</i> if such a key does not
     *                        exist
     */
    Value nextkey(CodeRunner ctxt, Value db)
        throws Fail.Exception;

} // end interface 'DbmImplementation'
